package operator

import (
	"gitee.com/bupt-zkjc/fabric2.2-interface/config_yaml"
	"gitee.com/bupt-zkjc/fabric2.2-interface/constant"
	"gitee.com/bupt-zkjc/fabric2.2-interface/entity"
	fautil "gitee.com/bupt-zkjc/fabric2.2-interface/util"
	"path/filepath"
)

type ChainOperator struct {
}
func (l *ChainOperator) AddChain(chain entity.Chain) (string, error){

	name := chain.Name + constant.NextLine
	orgName := chain.OrgName + constant.NextLine
	ordererUrl := chain.OrdererUrl + constant.NextLine
	peerUrl := chain.PeerUrl + constant.NextLine
	cryptoPath := chain.CryptoPath + constant.NextLine

	/**
	使用脚本生成sdk配置文件，包含初始组织的的信息，
	所有输入参数保存到chainArgs.txt文件中 genInitFile.sh
	*/
	fautil.WriteFile(name + orgName + ordererUrl + peerUrl + cryptoPath)
	path, err := config_yaml.GetParentDirectory()
	if err != nil {
		logger.Error(err)
		return "Create chain failure", err
	}

	fautil.Exec_shell(constant.GenNewChain, filepath.Join(path, constant.Dirpath))
	logger.Info("SDK---Create chain successful")
	return "Create chain successful", nil
}